package com.moleq.mgpos.activities;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.moleq.adapter.DeptSettingListAdapter;
import com.moleq.adapter.SelectionAdapter;
import com.moleq.base.MoleQBaseActivity;
import com.moleq.dialog.AgeSelectionDialog;
import com.moleq.dialog.DeptSelectionDialog;
import com.moleq.dialog.MessageBox;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.itemsetting.tools.MGCreateFiles;
import com.moleq.logic.MoleQConstants;
import com.moleq.mgpos.widget.BeepImageButton;
import com.moleq.models.AdapterDataSourceBuilder;
import com.moleq.models.AgeSetting;
import com.moleq.models.Departmant;
import com.moleq.models.GlobalInfo;
import com.moleq.models.Group;
import com.moleq.models.SubDepartmant;
import com.moleq.service.AgeSettingService;
import com.moleq.service.DepartmantService;
import com.moleq.service.GroupSettingService;
import com.moleq.service.ServiceManager;
import com.moleq.service.SubDepartmantService;
import com.moleq.util.EditTextMaxLengthWatcher;

public class DepartmentSettingActivity extends MoleQBaseActivity {
	private List<Departmant> listitem;
	private List<SubDepartmant> listsubdeptitem;
	private List<Group> listgroup;
	private ListView list, lv_dialog_list, lv_choose, lv_sub_dept;
	private DeptSettingListAdapter listAdapter;
	private SelectionAdapter listAdapter_group;
	private SelectionAdapter chooseAdapter = null;
	private DeptSettingListAdapter listAdapter_subdept;
	private int ControlID;
	private Button btn_add, btn_close, btn_undo, btn_del, btn_save, btn_group, btn_dialog_ok, btn_dialog_cancel, btn_age;
	private EditText et_no, et_dep_name, et_dialog_add;
	private CheckBox cb_tax, cb_fs, cb_tax2, cb_tax3, cb_tax4, cb_tax5, cb_tax6, cb_scalable, cb_wic, cb_non_discountable, cb_cvv, cb_age_ver;
	private String SaveMode = "";
	private LinearLayout ll_list, ll_title, ll_detail;
	private BeepImageButton iv_dialog_add, iv_dialog_ok, iv_dialog_delete, iv_dialog_update;
	private AbsoluteLayout al_dialog_perview_mode_layout;
	public String action = "", ls_group = "", ls_dept = "", ls_original_group, ls_age_no = "", ls_original_age, ls_original_name;
	private int new_no = 1;
	public String temp = "";
	private MessageDialog mMessageDialog;
	private AgeSettingService mAgeSettingService;
	private GroupSettingService mGroupSettingService;
	private SubDepartmantService mSubDeptService;
	private DepartmantService mDepartmantService;
	private MGCreateFiles mMGCreateFiles = new MGCreateFiles();
	
	private EditText et_subdept_no, et_subdept_name;
	private CheckBox cb_subdept_tax, cb_subdept_fs, cb_subdept_tax2, cb_subdept_tax3, cb_subdept_tax4, cb_subdept_tax5, cb_subdept_tax6, cb_subdept_scalable, cb_subdept_wic, cb_subdept_non_discountable, cb_subdept_cvv, cb_subdept_age_ver;
	private Button btn_dept;
	private LinearLayout ll_dept, ll_sub_dept;
	private Button btn_subdept_age;
	private DeptSelectionDialog mDeptSelectionDialog;
	public static final int DEPARTMENT_MODE = 0;
	public static final int SUBDEPT_MODE = 1;
	public static int CurrentMode = 0;
	public static final int DIALOG_ADD = 0;
	public static final int DIALOG_OK = 1;
	public static int CurrentActionStatus = 0;
	public static int deptmode = 0;

	public AgeSelectionDialog ageDialog;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dept_setting);
		
		et_no = (EditText) findViewById(R.id.et_dep_no);
		et_no.setEnabled(false);
		et_dep_name = (EditText) findViewById(R.id.et_dep_name);
		et_dep_name.addTextChangedListener(watcher);
		et_no.addTextChangedListener(watcher);
		et_no.addTextChangedListener(new EditTextMaxLengthWatcher(4, et_no, DepartmentSettingActivity.this));
		et_dep_name.addTextChangedListener(new EditTextMaxLengthWatcher(50, et_dep_name, DepartmentSettingActivity.this));

		et_subdept_no = (EditText) findViewById(R.id.et_subdept_no);
		et_subdept_no.setEnabled(false);
		et_subdept_name = (EditText) findViewById(R.id.et_subdept_name);
		et_subdept_name.addTextChangedListener(watcher);
		et_subdept_no.addTextChangedListener(watcher);
		et_subdept_no.addTextChangedListener(new EditTextMaxLengthWatcher(4, et_no, DepartmentSettingActivity.this));
		et_subdept_name.addTextChangedListener(new EditTextMaxLengthWatcher(50, et_subdept_name, DepartmentSettingActivity.this));

		ll_list = (LinearLayout) findViewById(R.id.ll_list);
		ll_title = (LinearLayout) findViewById(R.id.ll_title);
		ll_detail = (LinearLayout) findViewById(R.id.ll_detail);
		ll_dept = (LinearLayout) findViewById(R.id.ll_department);
		ll_sub_dept = (LinearLayout) findViewById(R.id.ll_sub_dept);

		cb_tax = (CheckBox) findViewById(R.id.cb_tax);
		cb_fs = (CheckBox) findViewById(R.id.cb_fs);
		cb_tax2 = (CheckBox) findViewById(R.id.cb_tax2);
		cb_tax3 = (CheckBox) findViewById(R.id.cb_tax3);
		cb_tax4 = (CheckBox) findViewById(R.id.cb_tax4);
		cb_tax5 = (CheckBox) findViewById(R.id.cb_tax5);
		cb_tax6 = (CheckBox) findViewById(R.id.cb_tax6);
		cb_scalable = (CheckBox) findViewById(R.id.cb_sb);
		cb_wic = (CheckBox) findViewById(R.id.cb_wic);
		cb_non_discountable = (CheckBox) findViewById(R.id.cb_discountable);
		cb_cvv = (CheckBox) findViewById(R.id.cb_cvv);
		cb_age_ver = (CheckBox) findViewById(R.id.cb_age_ver);

		cb_fs.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_tax.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_tax2.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_tax3.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_tax4.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_tax5.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_tax6.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_scalable.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_wic.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_non_discountable.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_cvv.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_age_ver.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_age_ver.setOnClickListener(onClickListener);

		cb_subdept_tax = (CheckBox) findViewById(R.id.cb_subdept_tax);
		cb_subdept_fs = (CheckBox) findViewById(R.id.cb_subdept_fs);
		cb_subdept_tax2 = (CheckBox) findViewById(R.id.cb_subdept_tax2);
		cb_subdept_tax3 = (CheckBox) findViewById(R.id.cb_subdept_tax3);
		cb_subdept_tax4 = (CheckBox) findViewById(R.id.cb_subdept_tax4);
		cb_subdept_tax5 = (CheckBox) findViewById(R.id.cb_subdept_tax5);
		cb_subdept_tax6 = (CheckBox) findViewById(R.id.cb_subdept_tax6);
		cb_subdept_scalable = (CheckBox) findViewById(R.id.cb_subdept_sb);
		cb_subdept_wic = (CheckBox) findViewById(R.id.cb_subdept_wic);
		cb_subdept_non_discountable = (CheckBox) findViewById(R.id.cb_subdept_discountable);
		cb_subdept_cvv = (CheckBox) findViewById(R.id.cb_subdept_cvv);
		cb_subdept_age_ver = (CheckBox) findViewById(R.id.cb_subdept_age_ver);

		cb_subdept_fs.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_subdept_tax.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_subdept_tax2.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_subdept_tax3.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_subdept_tax4.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_subdept_tax5.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_subdept_tax6.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_subdept_scalable.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_subdept_wic.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_subdept_non_discountable.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_subdept_cvv.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_subdept_age_ver.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_subdept_age_ver.setOnClickListener(onClickListener);

		btn_add = (Button) findViewById(R.id.btn_add);
		btn_undo = (Button) findViewById(R.id.btn_undo);
		btn_del = (Button) findViewById(R.id.btn_del);
		btn_save = (Button) findViewById(R.id.btn_update);
		btn_close = (Button) findViewById(R.id.btn_close);
		btn_group = (Button) findViewById(R.id.btn_group_name);
		btn_dept = (Button) findViewById(R.id.btn_subdept_dept);
		btn_age = (Button) findViewById(R.id.btn_age);
		btn_subdept_age = (Button) findViewById(R.id.btn_subdept_age);

		btn_add.setOnClickListener(onClickListener);
		btn_close.setOnClickListener(onClickListener);
		btn_undo.setOnClickListener(onClickListener);
		btn_del.setOnClickListener(onClickListener);
		btn_save.setOnClickListener(onClickListener);
		btn_group.setOnClickListener(onClickListener);
		btn_group.addTextChangedListener(watcher);
		btn_dept.setOnClickListener(onClickListener);
		btn_dept.addTextChangedListener(watcher);
		btn_age.setOnClickListener(onClickListener);
		btn_age.addTextChangedListener(watcher);
		btn_subdept_age.setOnClickListener(onClickListener);
		btn_subdept_age.addTextChangedListener(watcher);

		ll_list.setOnClickListener(onClickListener);
		ll_title.setOnClickListener(onClickListener);
		ll_detail.setOnClickListener(onClickListener);

		mAgeSettingService = ServiceManager.getInstance().mAgeSettingService;
		mDepartmantService = ServiceManager.getInstance().mDepartmantService;
		mGroupSettingService = ServiceManager.getInstance().mGroupSettingService;
		mSubDeptService = ServiceManager.getInstance().mSubDepartmantService;

		lv_choose = (ListView) findViewById(R.id.lv_choose);
		
		ArrayList<String> selectionItems = new ArrayList<String>();
		selectionItems.add("Department");
		selectionItems.add("Sub-dept");
		
		chooseAdapter = new SelectionAdapter(this, selectionItems);
		chooseAdapter.setItemTextSize(20);
		lv_choose.setAdapter(chooseAdapter);
		lv_choose.setOnItemClickListener(onItemClickListener);
		chooseAdapter.ScrollToRow(0);
		
		DataBinding();
		list.setOnItemClickListener(onItemClickListener);
		lv_sub_dept.setOnItemClickListener(onItemClickListener);
		RefresItem(0);
		RefresSubItem(0);
		SetEnabledTrue();
	}

	public void DataBinding() {

		SetListView();

		list = (ListView) findViewById(R.id.lsv_list);
		listAdapter = new DeptSettingListAdapter(this,AdapterDataSourceBuilder.readValueFromDept( listitem));
		list.setAdapter(listAdapter);

		lv_sub_dept = (ListView) findViewById(R.id.lsv_subdep_list);
		listAdapter_subdept = new DeptSettingListAdapter(this,AdapterDataSourceBuilder.readValueFromSubDept(listsubdeptitem) );
		lv_sub_dept.setAdapter(listAdapter_subdept);

		// lv_sub_dept.setDividerHeight(0);
		// list.setDividerHeight(0);
		mDeptSelectionDialog = new DeptSelectionDialog(DepartmentSettingActivity.this, this);

	}

	public OnItemClickListener onItemClickListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			// TODO Auto-generated method stub

			switch (parent.getId()) {
			case R.id.lv_choose:
				chooseAdapter.ScrollToRow(position);
				switch (position) {
				case DEPARTMENT_MODE:
					ll_dept.setVisibility(View.VISIBLE);
					ll_sub_dept.setVisibility(View.GONE);
					CurrentMode = DEPARTMENT_MODE;
					listitem = mDepartmantService.FindAll();
					listAdapter.setSource(AdapterDataSourceBuilder.readValueFromDept(listitem));
					listAdapter.notifyDataSetChanged();
					RefresItem(0);
					break;
				case SUBDEPT_MODE:
					ll_dept.setVisibility(View.GONE);
					ll_sub_dept.setVisibility(View.VISIBLE);
					CurrentMode = SUBDEPT_MODE;
					listsubdeptitem = mSubDeptService.FindAll();
					listAdapter_subdept.setSource(AdapterDataSourceBuilder.readValueFromSubDept(listsubdeptitem));
					listAdapter_subdept.notifyDataSetChanged();
					RefresSubItem(0);
					break;
				}
				Keyboard();
				chooseAdapter.ScrollToRow(position);
				break;
			case R.id.lsv_list:
				deptmode = 0;
				Keyboard();
				listAdapter.ScrollToRow(position);
				RefresItem(position);
				deptmode = 1;
				break;
			case R.id.lsv_subdep_list:
				deptmode = 0;
				Keyboard();
				listAdapter_subdept.ScrollToRow(position);
				RefresSubItem(position);
				deptmode = 1;
				break;
			default:
				break;
			}

		}

	};

	OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			// TODO Auto-generated method stub
			// if (deptmode== 1 && listitem.size() > 0) {
			// SetEnabledFalse();
			// }
			switch (CurrentMode) {
			case DEPARTMENT_MODE:
				if (deptmode == 1 && listitem.size() > 0) {
					SetEnabledFalse();// Fasle
				}
				break;
			case SUBDEPT_MODE:
				if (deptmode == 1 && listsubdeptitem.size() > 0) {
					SetEnabledFalse();// Fasle
				}
				break;
			}
		}
	};

	public OnClickListener onClickListener = new OnClickListener() {

		public void onClick(View view) {

			switch (view.getId()) {
			case R.id.ll_title:
				Keyboard();
				break;
			case R.id.ll_list:
				Keyboard();
				break;
			case R.id.ll_detail:
				Keyboard();
				break;
			case R.id.btn_add:
				SetEnabledFalse();// False
				ControlEmpty();
				SaveMode = "Insert";
				ls_group = "";
				ls_dept = "";
				break;

			case R.id.btn_close:
				SaveMode = "";
				ShowMessage(getResources().getString(R.string.msg_exit_program), MessageDialog.MESSAGE_BOX_YES_NO, MessageDialog.ICON_INFO);
				break;
			case R.id.btn_del:
				SaveMode = "Delete";
				Keyboard();
				switch (CurrentMode) {
				case DEPARTMENT_MODE:
					if (listitem.size() == 0)
						return;
					String dep_no = String.valueOf(listitem.get(Integer.valueOf(listAdapter.GetSelectedIndex())).getDep_no());
					if (mSubDeptService.checkdepbyid(dep_no)) {
						MessageBox.show(DepartmentSettingActivity.this, "Cannot delete a related department.");
						return;
					}
					break;
				case SUBDEPT_MODE:
					if (listsubdeptitem.size() == 0)
						return;
					String sd_no = String.valueOf(listsubdeptitem.get(listAdapter_subdept.GetSelectedIndex()).getSd_id());
					if (mSubDeptService.checkHaveSubdep(sd_no)) {
						ShowMessage("Cannot delete a related department.", MessageDialog.MESSAGE_BOX_OK, MessageDialog.ICON_INFO);
						return;
					}
					break;
				}

				ShowMessage("Are you sure to delete this department?", MessageDialog.MESSAGE_BOX_YES_NO, MessageDialog.ICON_INFO);
				break;
			case R.id.btn_undo:
				SaveMode = "";
				// if (listAdapter == null) return;
				int index,
				size;
				switch (CurrentMode) {
				case DEPARTMENT_MODE:
					index = Integer.valueOf(listAdapter.GetSelectedIndex());
					size = listitem.size();
					if (index >= 0 && size > 0) {
						String dep_no = String.valueOf(listitem.get(index).getDep_no());
						deptmode = 0;
						GetItemDetail(dep_no);
						deptmode = 1;
					}
					et_no.setEnabled(false);
					break;
				case SUBDEPT_MODE:
					index = Integer.valueOf(listAdapter_subdept.GetSelectedIndex());
					size = listsubdeptitem.size();
					if (index >= 0 && size > 0) {
						String dep_no = String.valueOf(listsubdeptitem.get(index).getSd_id());
						deptmode = 0;
						GetSubDeptItemDetail(dep_no);
						deptmode = 1;
					}
					et_subdept_no.setEnabled(false);
					break;
				}
				SetEnabledTrue();

				break;
			case R.id.btn_update:
				deptmode = 0;
				switch (CurrentMode) {
				case DEPARTMENT_MODE:
					Save();
					break;
				case SUBDEPT_MODE:
					SaveSubDept();
					break;
				}
				Keyboard();
				deptmode = 1;
				break;
			case R.id.btn_group_name:
				ControlID = R.id.btn_group_name;
				DialogWindows("GROUP", String.valueOf(btn_group.getText()));
				break;
			case R.id.btn_subdept_dept:
				ControlID = R.id.btn_subdept_dept;
				// DialogWindows("GROUP", String.valueOf(btn_group.getText()));
				if (mDeptSelectionDialog == null || mDeptSelectionDialog.isShowing())
					return;

				mDeptSelectionDialog.show(btn_dept.getText().toString());
				break;
			case R.id.btn_age:
				ControlID = R.id.btn_age;
				//AgeDialogWindows(String.valueOf(btn_age.getText()));
				ageDialog = new  AgeSelectionDialog(DepartmentSettingActivity.this);
				ageDialog.show(String.valueOf(btn_age.getText()));
				break;
			case R.id.btn_subdept_age:
				ControlID = R.id.btn_subdept_age;
				//AgeDialogWindows(String.valueOf(btn_subdept_age.getText()));
				ageDialog = new  AgeSelectionDialog(DepartmentSettingActivity.this);
				ageDialog.show(String.valueOf(btn_subdept_age.getText()));
				break;
			case R.id.cb_age_ver:
				if (cb_age_ver.isChecked()) {
					btn_age.setVisibility(View.VISIBLE);
				} else {
					btn_age.setVisibility(View.INVISIBLE);
				}
			case R.id.cb_subdept_age_ver:
				if (cb_subdept_age_ver.isChecked()) {
					btn_subdept_age.setVisibility(View.VISIBLE);
				} else {
					btn_subdept_age.setVisibility(View.INVISIBLE);
				}
				break;
			}
		}
	};
	private String ls_sub_age_no;

	public void SetListView() {
		listitem = mDepartmantService.FindAll();
		listsubdeptitem = mSubDeptService.FindAll();
	}

	public void SetGroupView() {
		listgroup = mGroupSettingService.FindAll();
	}

	public void SetEnabledFalse() {
		btn_add.setEnabled(false);
		btn_del.setEnabled(false);
		btn_close.setEnabled(false);
		list.setEnabled(false);
		btn_undo.setEnabled(true);
		btn_save.setEnabled(true);
		lv_sub_dept.setEnabled(false);
		lv_choose.setEnabled(false);
	}

	public void SetEnabledTrue() {
		btn_add.setEnabled(true);
		btn_del.setEnabled(true);
		btn_close.setEnabled(true);
		list.setEnabled(true);
		btn_undo.setEnabled(false);
		btn_save.setEnabled(false);
		lv_sub_dept.setEnabled(true);
		lv_choose.setEnabled(true);
	}

	public void ControlEmpty() {
		switch (CurrentMode) {
		case DEPARTMENT_MODE:
			et_no.setEnabled(true);
			et_dep_name.clearFocus();
			et_no.setFocusable(true);
			et_no.setText("");
			et_dep_name.setText("");
			cb_tax.setChecked(false);
			cb_fs.setChecked(false);
			cb_tax2.setChecked(false);
			cb_tax3.setChecked(false);
			cb_tax4.setChecked(false);
			cb_tax5.setChecked(false);
			cb_tax6.setChecked(false);
			cb_scalable.setChecked(false);
			cb_wic.setChecked(false);
			cb_non_discountable.setChecked(false);
			cb_cvv.setChecked(false);
			// cb_age.setChecked(false);
			btn_group.setText("");
			btn_age.setText("");
			ls_group = "";
			ls_age_no = "";
			break;
		case SUBDEPT_MODE:
			et_subdept_no.setEnabled(true);
			et_subdept_name.clearFocus();
			et_subdept_no.setFocusable(true);
			et_subdept_no.setText("");
			et_subdept_name.setText("");
			cb_subdept_tax.setChecked(false);
			cb_subdept_fs.setChecked(false);
			cb_subdept_tax2.setChecked(false);
			cb_subdept_tax3.setChecked(false);
			cb_subdept_tax4.setChecked(false);
			cb_subdept_tax5.setChecked(false);
			cb_subdept_tax6.setChecked(false);
			cb_subdept_scalable.setChecked(false);
			cb_subdept_wic.setChecked(false);
			cb_subdept_non_discountable.setChecked(false);
			cb_subdept_cvv.setChecked(false);
			// cb_age.setChecked(false);
			btn_dept.setText("");
			btn_subdept_age.setText("");
			ls_dept = "";
			ls_sub_age_no = "";
			break;
		}
	}

	public void Save() {
		String  wic,  cvv;
		int index;
		Departmant department = null;
		String dep_no = et_no.getText().toString();
		String name = et_dep_name.getText().toString();

		if (cb_wic.isChecked()) {
			wic = "1";
		} else {
			wic = "0";
		}

		if (cb_cvv.isChecked()) {
			cvv = "1";
		} else {
			cvv = "0";
		}
		if (ls_age_no == null || ls_age_no.equals("")) {
			ls_age_no = "0";
		}

		if (ls_group == null || ls_group.equals("")) {
			ShowMessage("The Group no can not be empty.", MessageDialog.MESSAGE_BOX_OK, MessageDialog.ICON_INFO);
			return;
		}

		if (dep_no == null || dep_no == "" || dep_no.trim().length() <= 0) {
			ShowMessage("The Department no can not be empty.", MessageDialog.MESSAGE_BOX_OK, MessageDialog.ICON_INFO);
			return;
		}

		if (name == null || name == "" || name.trim().length() <= 0) {
			ShowMessage("The Department name can not be empty.", MessageDialog.MESSAGE_BOX_OK, MessageDialog.ICON_INFO);
			return;
		}

		if (cb_age_ver.isChecked()) {
			if (btn_age.getText().equals("")) {
				ShowMessage("Please select the age.", MessageDialog.MESSAGE_BOX_OK, MessageDialog.ICON_INFO);
				return;
			}
			if (mAgeSettingService.CheckAge(ls_age_no)) {
				ls_age_no = "0";
			}
		} else {
			ls_age_no = "0";
		}

		department = new Departmant(Integer.valueOf(dep_no), name, cb_fs.isChecked(), cb_tax.isChecked(), cb_tax2.isChecked(), cb_tax3.isChecked(), cb_tax4.isChecked(), cb_tax5.isChecked(), cb_tax6.isChecked(), cb_scalable.isChecked(), wic, cb_non_discountable.isChecked(), cvv, Integer.valueOf(ls_group), Integer.valueOf(ls_age_no),null,null);

		if (SaveMode == "Insert") {

			if (mDepartmantService.isHaveDept(dep_no)) {

				ShowMessage("The Department was already exists.", MessageDialog.MESSAGE_BOX_OK, MessageDialog.ICON_INFO);
				return;
			}

			new_no = 1;
			new_no = Integer.valueOf(dep_no);
			mDepartmantService.insert(department);
			// ShowMessage("Save completed.", MessageDialog.MESSAGE_BOX_OK,
			// MessageDialog.ICON_INFO);
			Toast.makeText(DepartmentSettingActivity.this, "Save completed.", Toast.LENGTH_SHORT).show();

			et_no.setEnabled(false);
			SetEnabledTrue();
			if (SaveMode.equals("Insert"))
				SaveMode = "";
			DataBinding();
			index = listitem.size() - 1;
			// index = dep_index - 1;
			if (GlobalInfo.getInstance().isMGNetwork) {
				Departmant depSettingx = mDepartmantService.Find(new_no);
				mMGCreateFiles.createrFile(depSettingx.toMGData("INSERT"));//UPDATE,DELETE
			}
		} else {

			index = listAdapter.GetSelectedIndex();

			mDepartmantService.update(department);
			DataBinding();
			listAdapter.notifyDataSetChanged();
			SetEnabledTrue();

			// ShowMessage("Save completed.", MessageDialog.MESSAGE_BOX_OK,
			// MessageDialog.ICON_INFO);
			Toast.makeText(DepartmentSettingActivity.this, "Save completed.", Toast.LENGTH_SHORT).show();
			
			if (GlobalInfo.getInstance().isMGNetwork) {
				Departmant depSettingx = mDepartmantService.Find(Integer.valueOf(dep_no));
				mMGCreateFiles.createrFile(depSettingx.toMGData("UPDATE"));//UPDATE,DELETE
			}
		}

		RefresItem(index);
		SetEnabledTrue();// True
	}

	public void SaveSubDept() {
		String  wic,  cvv;
		int index;
		SubDepartmant subdepartment = null;
		String dep_no = et_subdept_no.getText().toString();
		String name = et_subdept_name.getText().toString();

		if (cb_subdept_wic.isChecked()) {
			wic = "1";
		} else {
			wic = "0";
		}

		if (cb_subdept_cvv.isChecked()) {
			cvv = "1";
		} else {
			cvv = "0";
		}
		if (ls_sub_age_no == null || ls_sub_age_no.equals("")) {
			ls_sub_age_no = "0";
		}

		if (ls_dept == null || ls_dept.equals("")) {
			ShowMessage("The Department no can not be empty.", MessageDialog.MESSAGE_BOX_OK, MessageDialog.ICON_INFO);
			return;
		}

		if (dep_no == null || dep_no == "" || dep_no.trim().length() <= 0) {
			ShowMessage("The Sub Department no can not be empty.", MessageDialog.MESSAGE_BOX_OK, MessageDialog.ICON_INFO);
			return;
		}

		if (name == null || name == "" || name.trim().length() <= 0) {

			ShowMessage("The Sub Department name can not be empty.", MessageDialog.MESSAGE_BOX_OK, MessageDialog.ICON_INFO);
			return;
		}

		if (cb_subdept_age_ver.isChecked()) {
			if (btn_age.getText().equals("")) {
				ShowMessage("Please select the age.", MessageDialog.MESSAGE_BOX_OK, MessageDialog.ICON_INFO);
				return;
			}
			if (mAgeSettingService.CheckAge(ls_sub_age_no)) {
				ls_sub_age_no = "0";
			}
		} else {
			ls_sub_age_no = "0";
		}

		subdepartment = new SubDepartmant(Integer.valueOf(dep_no), name, cb_subdept_fs.isChecked(), cb_subdept_tax.isChecked(), cb_subdept_tax2.isChecked(), cb_subdept_tax3.isChecked(), cb_subdept_tax4.isChecked(), cb_subdept_tax5.isChecked(), cb_subdept_tax6.isChecked(), cb_subdept_scalable.isChecked(), wic, cb_subdept_non_discountable.isChecked(), cvv, Integer.valueOf(ls_dept), Integer.valueOf(ls_sub_age_no),null,null);

		if (SaveMode == "Insert") {
			if (mSubDeptService.isHaveDept(dep_no)) {

				ShowMessage("The Sub Department was already exists.", MessageDialog.MESSAGE_BOX_OK, MessageDialog.ICON_INFO);
				return;
			}

			new_no = 1;
			new_no = Integer.valueOf(dep_no);
			mSubDeptService.insert(subdepartment);
			// ShowMessage("Save completed.", MessageDialog.MESSAGE_BOX_OK,
			// MessageDialog.ICON_INFO);
			Toast.makeText(DepartmentSettingActivity.this, "Save completed.", Toast.LENGTH_SHORT).show();

			et_no.setEnabled(false);
			SetEnabledTrue();
			if (SaveMode.equals("Insert"))
				SaveMode = "";
			DataBinding();
			index = listsubdeptitem.size() - 1;
			// index = subdept_index - 1;
			
			if (GlobalInfo.getInstance().isMGNetwork) {
				SubDepartmant subdepSettingx = mSubDeptService.Find(Integer.valueOf(new_no) );
				mMGCreateFiles.createrFile(subdepSettingx.toMGData("INSERT"));//UPDATE,DELETE
			}

		} else {

			index = listAdapter_subdept.GetSelectedIndex();

			mSubDeptService.update(subdepartment);
			DataBinding();
			listAdapter_subdept.notifyDataSetChanged();
			SetEnabledTrue();

			// ShowMessage("Save completed.", MessageDialog.MESSAGE_BOX_OK,
			// MessageDialog.ICON_INFO);
			Toast.makeText(DepartmentSettingActivity.this, "Save completed.", Toast.LENGTH_SHORT).show();
			
			if (GlobalInfo.getInstance().isMGNetwork) {
				SubDepartmant subdepSettingx = mSubDeptService.Find(subdepartment.getSd_id());
				mMGCreateFiles.createrFile(subdepSettingx.toMGData("UPDATE"));//UPDATE,DELETE
			}
		}

		RefresSubItem(index);
		SetEnabledTrue();// True
	}

	public void RefresItem(int position) {
		String dep_no = "";
		
		if (listitem.size() > 0) {
			listAdapter.ScrollToRow(position);
			list.setSelection(position);
			dep_no = String.valueOf(listitem.get(position).getDep_no());
			if (mDepartmantService.FindDetail(dep_no) != null) {
				deptmode = 0;
				GetItemDetail(dep_no);
				deptmode = 1;
			}else{
				listitem = mDepartmantService.FindAll();
				listAdapter.setSource(AdapterDataSourceBuilder.readValueFromDept(listitem));
				listAdapter.notifyDataSetChanged();
				if(position - 1 >= 0)listAdapter.ScrollToRow(position - 1);
			}
			
		}
	}

	private void RefresSubItem(int position) {
		// TODO Auto-generated method stub
		String dep_no = "";
		if (listsubdeptitem.size() > 0) {
			//listAdapter_subdept.setSource(listsubdeptitem);
			listAdapter_subdept.ScrollToRow(position);
			lv_sub_dept.setSelection(position);
			dep_no = String.valueOf(listsubdeptitem.get(position).getSd_id());
			if (mSubDeptService.FindDetail(dep_no) != null) {
				deptmode = 0;
				GetSubDeptItemDetail(dep_no);
				deptmode = 1;
			}else{
				listsubdeptitem = mSubDeptService.FindAll();
				listAdapter_subdept.setSource(AdapterDataSourceBuilder.readValueFromSubDept(listsubdeptitem));
				listAdapter_subdept.notifyDataSetChanged();
				if(position - 1 >= 0)listAdapter_subdept.ScrollToRow(position - 1);
			}
			
			
		}
	}

	public void GetItemDetail(String dep_no) {
		Departmant dep = mDepartmantService.FindDetail(dep_no);

		if (dep == null)
			return;

		et_no.setText(String.valueOf(dep.getDep_no()));
		et_dep_name.setText(dep.getDep_name());
		cb_tax.setChecked(dep.getTax1());
		cb_fs.setChecked(dep.getFood_stamp());
		cb_tax2.setChecked(dep.getTax2());
		cb_tax3.setChecked(dep.getTax3());
		cb_tax4.setChecked(dep.getTax4());
		cb_scalable.setChecked(dep.getScale_weight());
		if (dep.getWic().equals("1")) {
			cb_wic.setChecked(true);
		} else {
			cb_wic.setChecked(false);
		}

		cb_non_discountable.setChecked(dep.getNo_discnt());
		if (dep.getCvv().equals("1")) {
			cb_cvv.setChecked(true);
		} else {
			cb_cvv.setChecked(false);
		}

		if (dep.getGroup_no() == 0) {
			ls_group = "";
			btn_group.setText("");
		} else {
			ls_group = String.valueOf(dep.getGroup_no());
			btn_group.setText(mDepartmantService.Getgroupbyid(ls_group));

		}

		int age_no = dep.getAge();
		String age_desc = dep.getAge_desc();
		if (age_no <= 0) {
			ls_age_no = "";
			btn_age.setText("");
			btn_age.setVisibility(View.INVISIBLE);
			cb_age_ver.setChecked(false);
		} else {
			if (age_desc != null && !age_desc.equals("")) {
				btn_age.setVisibility(View.VISIBLE);
				btn_age.setText(String.valueOf(age_desc));
				ls_age_no = String.valueOf(age_no);
				cb_age_ver.setChecked(true);
			}
		}
		cb_tax5.setChecked(dep.getTax5());
		cb_tax6.setChecked(dep.getTax6());

	}

	public void GetSubDeptItemDetail(String dep_no) {
		SubDepartmant dep = mSubDeptService.FindDetail(dep_no);

		if (dep == null)
			return;
		et_subdept_no.setText(String.valueOf(dep.getSd_id()));
		et_subdept_name.setText(dep.getSd_name());
		cb_subdept_tax.setChecked(dep.getTax1());
		cb_subdept_fs.setChecked(dep.getFood_stamp());
		cb_subdept_tax2.setChecked(dep.getTax2());
		cb_subdept_tax3.setChecked(dep.getTax3());
		cb_subdept_tax4.setChecked(dep.getTax4());
		cb_subdept_scalable.setChecked(dep.getScale_weight());
		if (dep.getWic().equals("1")) {
			cb_subdept_wic.setChecked(true);
		} else {
			cb_subdept_wic.setChecked(false);
		}

		cb_subdept_non_discountable.setChecked(dep.getNo_discnt());
		if (dep.getCvv().equals("1")) {
			cb_subdept_cvv.setChecked(true);
		} else {
			cb_subdept_cvv.setChecked(false);
		}

		if (dep.getF03() == 0) {
			ls_dept = "";
			btn_dept.setText("");
		} else {
			ls_dept = String.valueOf(dep.getF03());
			btn_dept.setText(mSubDeptService.Getdepbyid(ls_dept));
			;
		}

		int age_no = dep.getAge();
		String age_desc = dep.getAge_desc();
		if (age_no <= 0) {
			ls_sub_age_no = "";
			btn_subdept_age.setText("");
			btn_subdept_age.setVisibility(View.INVISIBLE);
			cb_subdept_age_ver.setChecked(false);
		} else {
			if (age_desc != null && !age_desc.equals("")) {
				btn_subdept_age.setVisibility(View.VISIBLE);
				btn_subdept_age.setText(String.valueOf(age_desc));
				ls_sub_age_no = String.valueOf(age_no);
				cb_subdept_age_ver.setChecked(true);
			}
		}
		cb_subdept_tax5.setChecked(dep.getTax5());
		cb_subdept_tax6.setChecked(dep.getTax6());

	}

	public String getdatetime() {

		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		Date curDate = new Date(System.currentTimeMillis());
		String str = formatter.format(curDate);
		return str;
	};

	Dialog DialogWindow = null;

	private void DialogWindows(String Group, String value) {
		if (DialogWindow == null) {
			DialogWindow = new Dialog(this, R.style.dialog2);
			DialogWindow.setContentView(R.layout.dept_dialog_perview_models);
		}
		DialogWindow.show();
		lv_dialog_list = (ListView) DialogWindow.findViewById(R.id.lv_dialog_list);
		btn_dialog_ok = (Button) DialogWindow.findViewById(R.id.btn_dialog_ok);
		btn_dialog_cancel = (Button) DialogWindow.findViewById(R.id.btn_dialog_cancel);
		iv_dialog_add = (BeepImageButton) DialogWindow.findViewById(R.id.iv_dialog_add);
		iv_dialog_ok = (BeepImageButton) DialogWindow.findViewById(R.id.iv_dialog_ok);
		iv_dialog_delete = (BeepImageButton) DialogWindow.findViewById(R.id.iv_dialog_delete);
		iv_dialog_update = (BeepImageButton) DialogWindow.findViewById(R.id.iv_dialog_update);
		et_dialog_add = (EditText) DialogWindow.findViewById(R.id.et_dialog_add);
		al_dialog_perview_mode_layout = (AbsoluteLayout) DialogWindow.findViewById(R.id.al_dialog_perview_mode_layout);

		lv_dialog_list.setOnItemClickListener(onItemClickDialogWindowListener);
		btn_dialog_ok.setOnClickListener(OnClickDialogWindowListener);
		btn_dialog_cancel.setOnClickListener(OnClickDialogWindowListener);
		iv_dialog_add.setOnClickListener(OnClickDialogWindowListener);
		iv_dialog_ok.setOnClickListener(OnClickDialogWindowListener);
		iv_dialog_delete.setOnClickListener(OnClickDialogWindowListener);
		iv_dialog_update.setOnClickListener(OnClickDialogWindowListener);
		al_dialog_perview_mode_layout.setOnClickListener(OnClickDialogWindowListener);

		SetGroupView();

		lv_dialog_list.setTag(String.valueOf(Group));

		
		listAdapter_group = new SelectionAdapter(this,AdapterDataSourceBuilder.readValueFromGroup( listgroup));
		lv_dialog_list.setAdapter(listAdapter_group);
		int position = GetGroupPosition(btn_group.getText().toString());
		if(listgroup.size() > 0 && position == -1)position = 0;
		listAdapter_group.ScrollToRow(position);

	}
	
	public int GetGroupPosition(String DgName) {
		for (int i = 0; i < listgroup.size(); i++) {
			Group group = listgroup.get(i);
			if (group.getDg_name().equals(DgName)) {
				return i;
			}
		}
		return -1;
	}
	
	OnClickListener OnClickDialogWindowListener = new OnClickListener() {

		public void onClick(View view) {
			int index = -1;
			if (listAdapter_group != null) {
				index = listAdapter_group.GetSelectedIndex();
			}
			switch (view.getId()) {
			case R.id.btn_dialog_ok:
				if (listgroup.size() > 0) {

					String value = String.valueOf(listgroup.get(index).getDg_name());
					ls_group = String.valueOf(listgroup.get(index).getDg_no());
					Button btn_ScaleControl = (Button) findViewById(ControlID);
					btn_ScaleControl.setText(value);
				}
				DialogWindow.dismiss();
				break;	
			case R.id.btn_dialog_cancel:
				DialogWindow.dismiss();
				break;
			case R.id.iv_dialog_update:
				if (listgroup.size() <= 0) {
					return;
				}
				action = "update";
				DialogUpdate(index);
				String value = String.valueOf(listgroup.get(index).getDg_name());
				et_dialog_add.setText(value);
				ls_original_group = value;
				break;

			case R.id.iv_dialog_add:
				// SaveMode = "";

				DialogUpdate(index);
				et_dialog_add.setText("");
				break;
			case R.id.iv_dialog_ok:
				DialogUpdate(index);
				et_dialog_add.setText("");
				break;

			case R.id.iv_dialog_delete:
				if (listgroup.size() <= 0)
					return;
				int position = listAdapter_group.GetSelectedIndex();
				String Value = String.valueOf(listgroup.get(position).getDg_no());

				if (mGroupSettingService.Checkgroupbyid(Value) == false) {
					ShowMessage("Cannot delete a related group.", MessageDialog.MESSAGE_BOX_OK, MessageDialog.ICON_INFO);
					return;
				}
				temp = SaveMode;
				SaveMode = "Dialog_Delete";
				ShowMessage("Are you sure to delete this group?", MessageDialog.MESSAGE_BOX_YES_NO, MessageDialog.ICON_INFO);
				break;

			case R.id.al_dialog_perview_mode_layout:
				Keyboard();
				break;
			}
		}

	};

	OnItemClickListener onItemClickDialogWindowListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			// TODO Auto-generated method stub
			listAdapter_group.ScrollToRow(position);
		}

	};

	
	public void DialogUpdate(int index) {
		CurrentActionStatus = Integer.valueOf((String) iv_dialog_add.getTag());
		if (CurrentActionStatus == DIALOG_ADD) {
			lv_dialog_list.setEnabled(false);
			btn_dialog_ok.setEnabled(false);
			btn_dialog_cancel.setEnabled(false);
			iv_dialog_delete.setEnabled(false);
			iv_dialog_update.setEnabled(false);
			et_dialog_add.setVisibility(View.VISIBLE);
			iv_dialog_ok.setVisibility(View.VISIBLE);
			iv_dialog_add.setVisibility(View.GONE);
			iv_dialog_add.setTag(String.valueOf(DIALOG_OK));
		} else if (CurrentActionStatus == DIALOG_OK) {
			lv_dialog_list.setEnabled(true);
			btn_dialog_ok.setEnabled(true);
			btn_dialog_cancel.setEnabled(true);
			iv_dialog_delete.setEnabled(true);
			iv_dialog_update.setEnabled(true);
			et_dialog_add.setVisibility(View.INVISIBLE);
			iv_dialog_ok.setVisibility(View.GONE);
			iv_dialog_add.setVisibility(View.VISIBLE);
			iv_dialog_add.setTag(String.valueOf(DIALOG_ADD));

			if (!action.equals("update")) {
				action = "";
			}

			String Value = String.valueOf(et_dialog_add.getText());
			String ls_dg_no = "";
			Group mGroup = null;
			if (listgroup.size() > 0) {
				ls_dg_no = String.valueOf(listgroup.get(index).getDg_no());
			}

			if (Value != null && !Value.trim().equals("")) {
				if (action.equals("update") && !ls_dg_no.equals("")) {
					action = "";
					if (ls_original_group.equals(Value)) {
						return;
					}

					if (mGroupSettingService.Checkgroup(Value) == false) {
						ShowMessage("The group was already exists.", MessageDialog.MESSAGE_BOX_OK, MessageDialog.ICON_INFO);
						return;
					}

					mGroupSettingService.UpdateGroup(Value, ls_dg_no);
					
					if (GlobalInfo.getInstance().isMGNetwork) {
						//Group itemSettingx = mGroupSettingService.Find();
						mGroup = mGroupSettingService.Find(Integer.valueOf(ls_dg_no));
						mMGCreateFiles.createrFile(mGroup.toMGData("UPDATE"));//UPDATE,DELETE
						}

				} else {

					if (mGroupSettingService.Checkgroup(Value) == false) {
						ShowMessage("The group was already exists.", MessageDialog.MESSAGE_BOX_OK, MessageDialog.ICON_INFO);
						return;
					}
					mGroupSettingService.InsertGroup(Value);

					if (GlobalInfo.getInstance().isMGNetwork) {
						//Group itemSettingx = mGroupSettingService.Find();
						mGroup = mGroupSettingService.Find(mGroupSettingService.getMaxNo());
						mMGCreateFiles.createrFile(mGroup.toMGData("INSERT"));//UPDATE,DELETE
					}
					

				}
				SetGroupView();
				listAdapter_group.setSource(AdapterDataSourceBuilder.readValueFromGroup(listgroup));
				listAdapter_group.notifyDataSetChanged();

			}

			listAdapter_group.ScrollToRow(listgroup.size() - 1);
			lv_dialog_list.setSelection(listgroup.size() - 1);
			// listAdapter_group.
		}

	}

	private TextWatcher watcher = new TextWatcher() {

		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
		}

		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			// TODO Auto-generated method stub
		}

		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// TODO Auto-generated method stub
			switch (CurrentMode) {
			case DEPARTMENT_MODE:
				if (deptmode == 1 && listitem.size() > 0) {
					SetEnabledFalse();// Fasle
				}
				break;
			case SUBDEPT_MODE:
				if (deptmode == 1 && listsubdeptitem.size() > 0) {
					SetEnabledFalse();// Fasle
				}
				break;
			}

		}
	};

	public void Delete() {

		// DataBinding();

		switch (CurrentMode) {
		case DEPARTMENT_MODE:
			String dep_no = String.valueOf(listitem.get(listAdapter.GetSelectedIndex()).getDep_no());
			if (GlobalInfo.getInstance().isMGNetwork) {
				Departmant depSettingx = mDepartmantService.Find(Integer.valueOf(dep_no) );
				mMGCreateFiles.createrFile(depSettingx.toMGData("DELETE"));//UPDATE,DELETE
			}
			mDepartmantService.Delete(dep_no);
			
			RefresItem(0);
			break;
		case SUBDEPT_MODE:
			
			String sd_no = String.valueOf(listsubdeptitem.get(listAdapter_subdept.GetSelectedIndex()).getSd_id());
			if (GlobalInfo.getInstance().isMGNetwork) {
				SubDepartmant subdepSettingx = mSubDeptService.Find(Integer.valueOf(sd_no) );
				mMGCreateFiles.createrFile(subdepSettingx.toMGData("DELETE"));//UPDATE,DELETE
			}
			mSubDeptService.Delete(sd_no);
			
			RefresSubItem(0);
			break;
		}
	}
	
	public void Keyboard() {
		InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		inputMethodManager.hideSoftInputFromWindow(DepartmentSettingActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
	}

	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub
			int index, index2 = -1;
			if (SaveMode.equals("Delete")) {
				switch (CurrentMode) {
				case DEPARTMENT_MODE:
					String dep_no = "";
					index = listAdapter.GetSelectedIndex();
					index2 = -1;
					if (index == 0 && listitem.size() > 1) {
						index2 = index;
						dep_no = String.valueOf(listitem.get(index2 + 1).getDep_no());
					} else if (index > 0) {
						index2 = index - 1;
						dep_no = String.valueOf(listitem.get(index2).getDep_no());
					}
					Delete();
					// DataBinding();
					listitem = mDepartmantService.FindAll();
					deptmode = 0;
					if (!dep_no.equals("")) {
						GetItemDetail(dep_no);
					} else {
						ControlEmpty();
						listitem.clear();
					}
					listAdapter.setSource(AdapterDataSourceBuilder.readValueFromDept(listitem));
					listAdapter.notifyDataSetChanged();
					deptmode = 1;
					listAdapter.ScrollToRow(index2);
					break;
				case SUBDEPT_MODE:
					String sd_no = "";
					index = listAdapter_subdept.GetSelectedIndex();
					index2 = -1;
					if (index == 0 && listsubdeptitem.size() > 1) {
						index2 = index;
						sd_no = String.valueOf(listsubdeptitem.get(index2 + 1).getSd_id());
					} else if (index > 0) {
						index2 = index - 1;
						sd_no = String.valueOf(listsubdeptitem.get(index2).getSd_id());
					}
					Delete();
					// DataBinding();
					listsubdeptitem = mSubDeptService.FindAll();
					deptmode = 0;
					if (!sd_no.equals("")) {
						GetSubDeptItemDetail(sd_no);
					} else {
						ControlEmpty();
						listsubdeptitem.clear();

					}
					listAdapter_subdept.setSource(AdapterDataSourceBuilder.readValueFromSubDept(listsubdeptitem));
					listAdapter_subdept.notifyDataSetChanged();
					deptmode = 1;
					listAdapter_subdept.ScrollToRow(index2);
					break;
				}
				mMessageDialog.dismiss();
			} else if (SaveMode.equals("Dialog_Delete")) {
				int position = listAdapter_group.GetSelectedIndex();
				String Value = String.valueOf(listgroup.get(position).getDg_no());
				if (GlobalInfo.getInstance().isMGNetwork) {
					Group g = mGroupSettingService.Find(listgroup.get(position).getDg_no());
					mMGCreateFiles.createrFile(g.toMGData("DELETE"));//UPDATE,DELETE
				}
				mGroupSettingService.DeleteGroup(Value);
				SetGroupView();
				listAdapter_group.setSource(AdapterDataSourceBuilder.readValueFromGroup(listgroup));
				listAdapter_group.notifyDataSetChanged();
				if (position > 0) {
					position = position - 1;
				}
				listAdapter_group.ScrollToRow(position);
				if (temp.equals("Insert")) {
					SaveMode = temp;
				}
				mMessageDialog.dismiss();
			} else {
				finish();
			}
			;
		}

		@Override
		public void onClickNo() {
			mMessageDialog.dismiss();
		}

		@Override
		public void onClickOK() {
			mMessageDialog.dismiss();
		}

	};

	public void ShowMessage(String ls_msg, int style, int icon) {
		if (mMessageDialog != null && mMessageDialog.isShowing()) {
			mMessageDialog.dismiss();
		}
		mMessageDialog = new MessageDialog(DepartmentSettingActivity.this);
		mMessageDialog.setStyle(style);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(ls_msg);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}

	@Override
	public void onReturnValue(int what, Object obj) {
		// TODO Auto-generated method stub
		switch (what) {
		case MoleQConstants.DIALOG_AGE_INFO:
			AgeSetting mAge = (AgeSetting) obj;
			switch (CurrentMode) {
				case DEPARTMENT_MODE:
				btn_age.setText(mAge.getAs_desc());
				ls_age_no = String.valueOf(mAge.getAs_no());
					break;
				case SUBDEPT_MODE:
				btn_subdept_age.setText(mAge.getAs_desc());
				ls_sub_age_no = String.valueOf(mAge.getAs_no());
					break;
				}
			break;
		case MoleQConstants.DIALOG_PRICE_DEPT_INFO:
			Departmant mDept = (Departmant) obj;
			btn_dept.setText(mDept.getDep_name());
			ls_dept = String.valueOf(mDept.getDep_no());
			break;
		default:
			break;
		}
		
	}

	
}